<template>
  <div class="dialog-container">
    <el-dialog :visible.sync="showAdd" width="80%" top="1%" :title="title" :before-close="handleClose">
      <el-form ref="settingEntity" :model="settingEntity">
        <div class="modal-content">
          <table class="table-container">
            <tbody>
              <tr>
                <td class="td-label-2">
                  <must/>作者</td>
                <td>
                  <el-input v-model="settingEntity.author" name="作者" v-validate="'required'" placeholder="请输入作者" />
                  <span v-show="errors.has('作者')" style="color: red;">{{ errors.first('作者') }}</span>
                </td>
                <td class="td-label-2">
                  <must/>邮箱</td>
                <td>
                  <el-input v-model="settingEntity.email" name="邮箱" v-validate="'required'" placeholder="请输入邮箱" />
                  <span v-show="errors.has('邮箱')" style="color: red;">{{ errors.first('邮箱') }}</span>
                </td>
              </tr>
              <tr>
                <td class="td-label-2">
                  <must/>后端包名</td>
                <td>
                  <el-input v-model="settingEntity.packageNm" name="后端包名" v-validate="'required'" placeholder="请输入后端包名" />
                  <span v-show="errors.has('后端包名')" style="color: red;">{{ errors.first('后端包名') }}</span>
                </td>
                <td class="td-label-2">
                  <must/>前端模块前缀</td>
                <td>
                  <el-input v-model="settingEntity.mainModule" name="前端模块前缀" v-validate="'required'" placeholder="请输入前端模块前缀" />
                  <span v-show="errors.has('前端模块前缀')" style="color: red;">{{ errors.first('前端模块前缀') }}</span>
                </td>
              </tr>
              <tr>
                <td class="td-label-2">可忽略表前缀</td>
                <td>
                  <el-input v-model="settingEntity.tablePrefix" placeholder="可忽略表前缀" />
                </td>
              </tr>
              <tr>
                <td class="td-label-2">
                  <must/>前端页面类型</td>
                <td colspan="3">
                  <el-radio-group v-model="settingEntity.uiType" name="前端页面类型" v-validate="'required'">
                    <el-radio-button v-for="(val, key) in uiTypeMap" :label="key" :key="key">{{val}}</el-radio-button>
                  </el-radio-group>
                  <span v-show="errors.has('前端页面类型')" style="color: red;">{{ errors.first('前端页面类型') }}</span>
                </td>
              </tr>
              <tr>
                <td class="td-label-2">
                  <must/>后端模板</td>
                <td colspan="3">
                  <el-checkbox-group v-model="javaTemplatesArr" name="后端模板" v-validate="'required'" fill="rgb(65 165 4)">
                    <el-checkbox-button v-for="(val, key) in tempMap" :label="key" :key="key">{{val}}</el-checkbox-button>
                  </el-checkbox-group>
                  <span v-show="errors.has('后端模板')" style="color: red;">{{ errors.first('后端模板') }}</span>
                </td>
              </tr>

            </tbody>
          </table>
        </div>
      </el-form>
      <div slot="footer">
        <el-button @click="handleClose" type="danger">关 闭</el-button>
        <el-button @click="handleSave" type="primary" :disabled="btnDisabled">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    downloadFile2Post
  } from '@/api/download';

  /* 工具 */
  import * as common from '@/utils/base/common';

  export default {
    /*  内容: 配置 */
    name: 'setting',
    data() {
      return {
        title: '自定义配置',
        btnDisabled: false, //按钮禁用状态
        settingEntity: {
          selectTables: '',
          author: 'superC',
          email: 'superc3707@gmail.com',
          packageNm: 'com.kim.',
          mainModule: '',
          tablePrefix: '',
          uiType: 3,
          javaTemplatesArr: '',
        },
        
        // 后端模板多选框
        uiTypeMap: {
          1: '只列表',
          2: '列表+增删改',
          3: '页面+Mixins',
          4: '页面(有图片)+Mixins'
        },
        tempMap: {
          1: 'controller',
          2: 'biz',
          3: 'entity',
          4: 'mapper.java',
          5: 'mapper.xml'
        },
        javaTemplatesArr: ['1','2','3','4','5']
        
      }
    },
    created() {
      this.initData()
    },
    props: ['restPath', 'showAdd', 'multipleSelection'],
    methods: {
      
      initData() {
        this.settingEntity.selectTables = this.multipleSelection
      },
      
      /**
       * 点击保存按钮
       */
      handleSave() {
        this.$validator.validateAll().then((result) => {
          if (result) {
            
            this.settingEntity.javaTemplatesArr = this.javaTemplatesArr
            downloadFile2Post(this, '/api/generator/codeGenerator/setSetting', this.settingEntity)
          }else{
            // 提示信息，并且防止连续多次请求
            common.tips(this, '请查看页面信息是否填写完整或正确！')
          }
        })
      },
      
      handleClose() {
        this.$emit('closeDialog') // 向父组件发送关闭事件
      }
      
    } // methods end
  }
</script>
<style scoped>
  @import "~@/styles/form.css";
</style>